#include <stdio.h>
#include <stdlib.h>

#include <string>
#include <vector>
#include <list>
#include <map>
#include <algorithm>
using namespace std;

#include "pugixml.hpp"
using namespace pugi;

string ReadText(const string& name)
{
	FILE* fp = fopen(name.c_str(), "rb");
	if (fp == NULL)
	{
		return NULL;
	}
	fseek(fp, 0, SEEK_END);
	int len = ftell(fp);
	fseek(fp, 0, SEEK_SET);
	char* buf = new char[len + 4];//+4 for c string \0
	memset(buf, 0, len + 4);
	if (fread(buf, 1, len, fp) != len)
	{
		delete buf;
		fclose(fp);
		return NULL;
	}
	fclose(fp);
	string str = buf;
	delete[] buf;
	return str;
}

int main(int argc, char* argv[])
{
	string str = ReadText("res/a.xml");

	xml_document doc;
	xml_parse_result result = doc.load_string(str.c_str());
	if (result.status != status_ok)
	{
		return -1;
	}

	pugi::xpath_node_set levelNodes = doc.select_nodes("/LevelList/Level");
	size_t levelCount = levelNodes.size();
	if (levelCount == 0)
	{
		return -1;
	}

	for (size_t i = 0; i < levelCount; ++i)
	{
		pugi::xml_node levelNode = levelNodes[i].node();

		int vertexCount = levelNode.attribute("vertexCount").as_int();
		int polygonCount = levelNode.attribute("polygonCount").as_int();
		int coverCount = levelNode.attribute("coverCount").as_int();
		int lineCount = levelNode.attribute("lineCount").as_int();
		string name = levelNode.attribute("name").as_string();

		string verticesStr = levelNode.child("Vertices").text().as_string();
		string linesStr = levelNode.child("Lines").text().as_string();
		string solveStr = levelNode.child("Solve").text().as_string();
		string edgesStr = levelNode.child("Edges").text().as_string();

		xpath_node_set polygonsNode = levelNode.select_nodes("PolygonList/Polygon");
		xpath_node_set coversNode = levelNode.select_nodes("CoverList/Cover");

		if (polygonCount != polygonsNode.size() || coverCount != coversNode.size())
		{
			return -1;
		}
	}

	return 0;
}

